﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/
using System.Collections.ObjectModel;
using System.Windows.Input;
using Peter.Common;
using Peter.Common.MainMenu;

namespace Peter.Logic
{
   /// <summary>
   /// A tool window for peter that has editable content and a menu.
   /// </summary>
   public class EditablePeterToolWindowWithMenu : EditablePeterToolWindow
   {
      private readonly ObservableCollection <IMainMenuItem> m_MenuItems;
      private ICommand m_CmdExecute;

      /// <summary>
      /// Initializes a new tool window object.
      /// </summary>
      public EditablePeterToolWindowWithMenu ()
      {
         this.m_MenuItems = new ObservableCollection <IMainMenuItem> ();
      }

      /// <summary>
      /// Gets the explorer context menu.
      /// </summary>
      public ObservableCollection<IMainMenuItem> ContextMenuItems { get { return this.m_MenuItems; } }

      /// <summary>
      /// Gets the execute command.
      /// </summary>
      public ICommand ExecuteCommand
      {
         get { return this.m_CmdExecute ?? (this.m_CmdExecute = new RelayCommand (this.OnExecute, this.CanExecute)); }
      }

      /// <summary>
      /// Updates the context of the menu items.
      /// </summary>
      /// <param name="context">The current context of the menu.</param>
      protected void UpdateMenuContext (object context)
      {
         foreach (var contextMenuItem in this.ContextMenuItems)
         {
            contextMenuItem.Context = context;
         }
      }

      /// <summary>
      /// Checks to see if the selected item can execute.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      /// <returns>True if command can execute, other wise false.</returns>
      protected virtual bool CanExecute (object obj)
      {
         return true;
      }

      /// <summary>
      /// Occurs when a tree item is being executed.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      protected virtual void OnExecute (object obj)
      {
      }
   }
}
